<?php
/** 
 * 分类管理
 */
namespace Admin\Controller;
use Admin\Controller\AdminController;
class CategoryController extends AdminController {
    
	//显示所有分类
    public function index(){
        $this->assign('title','分类目录');
        $cate = M('category')->field("id,pid,name,slug")->select();
        $this->assign('cate',get_sort_cate($cate));
    	$this->display();
    }
    //添加分类
    public function add(){
        if(!IS_POST)$this->error("提交方式错误!");
        $Cate = D('category');
        if(!$Cate->create()){
            $this->error($Cate->getError());
        }else{
            $Cate->add()?$this->success("添加成功"):$this->error("添加失败"); 
        }
    }  
    //删除分类
    public function del(){
        if(!IS_GET)$this->error("非法请求!");
        //如果分类中有子分类，禁止删除
        $cate = M('category')->field("id,pid,name,slug")->select();
        $cate_id=I('id');
        if(count(get_cate_child($cate,$cate_id))!=0){
            $this->error("请先删除子分类!");  
        } 
        //开始删除分类
        $result = M("category")->where("id=".I("id"))->delete();
        $result?$this->success("删除成功"):$this->error("禁止删除有文章的分类");
    }
    //编辑分类
    public function edit(){
        if(!IS_GET)$this->error("非法请求!");
        $result = M('category')->where("id=".I('id'))->select();
        $cate = M('category')->field("id,pid,name,slug")->select();;
        $this->assign('cate',get_sort_cate($cate));        
        $this->assign("result",$result);
    	$this->assign("title","编辑分类");
        $this->display();
    }
    //更新分类信息
    public function update(){
      //  p(I());
        if(!IS_POST) $this->error("非法请求");
        $result = M('category')->save(I());
        $result?$this->success("更新成功"):$this->error("更新失败");
    }
}